package com.jasonidea.dockerdemo.service.impl;

import com.jasonidea.dockerdemo.service.DockerDemoService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author Jason315
 * @date 2022-12-13 10:40:02
 */
@Service
public class DockerDemoServiceImpl implements DockerDemoService {

    private static final String KEY_VISIT_COUNT = "VISIT_COUNT";

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public Integer getVisitCount() {
        Integer count = (Integer) redisTemplate.opsForValue().get(KEY_VISIT_COUNT);
        if (Objects.isNull(count)) {
            count = 0;
        }
        return count;
    }

    @Override
    public void incrementVisitCount() {
        Integer visitCount = getVisitCount();
        if (Objects.isNull(visitCount)) {
            visitCount = 0;
        }
        redisTemplate.opsForValue().set(KEY_VISIT_COUNT, visitCount + 1);
    }
}
